package com.lyl.syt.api;

import com.lyl.syt.common.result.Result;
import com.lyl.syt.model.hosp.Department;
import com.lyl.syt.modules.mongodb.service.DepartmentService;
import com.lyl.syt.vo.hosp.DepartmentQueryVo;
import com.lyl.syt.vo.hosp.DepartmentVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController("ApiDepartmentController")
@RequestMapping("/api/hosp")
@RequiredArgsConstructor
public class DepartmentController extends BaseAPI{
    private final DepartmentService departmentService;

    @ApiOperation("根据医院编号获取科室")
    @GetMapping("/department/{hoscode}")
    public Result getDeptList(@PathVariable String hoscode){
        List<DepartmentVo> list = departmentService.findDeptTree(hoscode);
        return Result.ok(list);
    }

    @PostMapping("/department/remove")
    public Result<Boolean> removeDept(HttpServletRequest request){
        //获取传递的参数并验证签名
        Map<String, Object> paramMap = prefixHandler(request);
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //根据医院编号和科室编号移除指定科室
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    @PostMapping("/department/list")
    public Result<Page<Department>> findDeptList(HttpServletRequest request) {
        //获取传递的参数并验证签名
        Map<String, Object> paramMap = prefixHandler(request);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //分页参数
        int page = Integer.parseInt((String) paramMap.getOrDefault("page", 1));
        int limit = Integer.parseInt((String) paramMap.getOrDefault("limit", 5));

        //封装请求对象
        DepartmentQueryVo queryVo = new DepartmentQueryVo();
        queryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, queryVo);
        return Result.ok(pageModel);
    }

    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result<Boolean> saveDepartment(HttpServletRequest request) {
        //获取传递的参数并验证签名
        Map<String, Object> paramMap = prefixHandler(request);
        departmentService.save(paramMap);
        return Result.ok();
    }

}
